﻿using AdminSystem.Applications;
using AdminSystem.Domain.User;
using AdminSystem.Repositories;
using AdminSystem.Write.API.Command.SlaughtersCommand;
using MediatR;

namespace AdminSystem.Write.API.Handers.SlaughtersHander
{
    public class SlaughterQueryCommandsHandler : IRequestHandler<SlaughterQueryCommand, ApiResult<Slaughter>>
    {
        public SlaughterQueryCommandsHandler(IRepository<Slaughter> repository)
        {
            Repository = repository;
        }

        public IRepository<Slaughter> Repository { get; }
        /// <summary>
        /// 查询屠宰信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<Slaughter>> Handle(SlaughterQueryCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<Slaughter> result = new ApiResult<Slaughter>();
                result.statusCode = StatusCode.Success;
                result.Message = "查询成功";
                var list = Repository.GetAll();
                var entity = list.Result.FirstOrDefault();
                result.Data = entity;
                return Task.FromResult(result);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
